import { ref,  } from 'vue'
import { getTaskList,getTaskDetailByOne,getTaskDetailByTwo } from '@/api/server/taskMange'
import { useServerUserStore } from '@/stores/modules/user-server'
import { useRouter } from 'vue-router'
const userStore = useServerUserStore()

export default function useTaskManage() {
  const router = useRouter()

  const taskList = ref([])
  const page = ref(1)
  const pageSize = ref(10)
  const total = ref(0)
  const timeOrder = ref('desc') // 默认时间降序
  const status = ref('所有状态') // 默认全部批阅状态

  const handleReset =async () => {
    timeOrder.value = 'desc'
    status.value = '所有状态'
    await fetchTaskList(userStore.userInfo.id,timeOrder.value,status.value)
  }


  // 获取评阅任务列表
  const fetchTaskList = async (id,timeOrder,status) => {
    const res = await getTaskList({ id: id, page: page.value, pageSize: pageSize.value,timeOrder,status })  
    taskList.value = res.data.data.list
    total.value = res.data.data.total
    
  }

  const handleQuery = async () => {
    await fetchTaskList(userStore.userInfo.id,timeOrder.value,status.value)
  }

  // 获取整张试卷批阅任务详情
  const wholePaperPage = ref(1)
  const wholePaperPageSize = ref(10)
  const wholePaperTotal = ref(0)
  const doneTotal = ref(0)
  const wholePaperStatus = ref('所有状态')
  const taskDetailList = ref([])
  const uniquePaperIds = ref([])
  const fetchTaskDetailByOne = async (id,status) => {
    const res = await getTaskDetailByOne({ reviewAssignmentId: id, page: wholePaperPage.value, pageSize: wholePaperPageSize.value,status })
    taskDetailList.value = res.data.data.list
    wholePaperTotal.value = res.data.data.total
    doneTotal.value = res.data.data.doneTotal
    uniquePaperIds.value = res.data.data.uniquePaperIds
  }

  // 获取单道大题批阅任务详情
  const singleQuestionPage = ref(1)
  const singleQuestionPageSize = ref(10)
  const singleQuestionTotal = ref(0)
  const taskDetailListByTwo = ref([])
  const reviewAssignmentDetailIds = ref([])
  const fetchTaskDetailByTwo = async (id,status) => {
    const res = await getTaskDetailByTwo({ reviewAssignmentId: id, page: singleQuestionPage.value, pageSize: singleQuestionPageSize.value,status })
    taskDetailListByTwo.value = res.data.data.list
    singleQuestionTotal.value = res.data.data.total
    doneTotal.value = res.data.data.doneTotal
    reviewAssignmentDetailIds.value = res.data.data.reviewAssignmentDetailIds
  }

  const reviewAssignmentId = ref(0)
  // 跳转整张试卷批阅 --- 所有试卷
  const goToBatchReview =  (id) => {
    reviewAssignmentId.value = id
    router.push({
      name: '整张试卷批阅',
      query: {
        reviewAssignmentId: reviewAssignmentId.value,
        uniquePaperIds: uniquePaperIds.value,
        strategy: 'batch',
        isExpired: isExpired.value
      }
    })
  }

  // 判断批阅任务是否过期
  const isExpired = ref(false)
  // 跳转整张试卷批阅 --- 单张试卷
  const goToSinglePaperReview = (id,reviewAssignmentId) => {
    uniquePaperIds.value = [parseInt(id)]
    router.push({
      name: '整张试卷批阅',
      query: { 
        reviewAssignmentId: reviewAssignmentId, 
        uniquePaperIds: uniquePaperIds.value,
        strategy: 'single', 
        isExpired: isExpired.value
      }
    })
  }


  // 跳转单道大题批量批阅
  const goToSingleQuestionReview = (reviewAssignmentId,reviewAssignmentDetailIds) => {
    router.push({
      name: '单道大题批阅',
      query: { 
        reviewAssignmentId: reviewAssignmentId,
        reviewAssignmentDetailIds: reviewAssignmentDetailIds,
        isExpired: isExpired.value,
        strategy: 'batch'
      }
    })
  } 

  // 跳转单道大题单张试卷批阅 
  const goToSingleQuestionSinglePaperReview = (reviewAssignmentId,reviewAssignmentDetailId) => { 
    router.push({
      name: '单道大题批阅',
      query: { 
        reviewAssignmentId: reviewAssignmentId,
        reviewAssignmentDetailIds: [reviewAssignmentDetailId],
        strategy: 'single',
        isExpired: isExpired.value
      }
    })
  }


  return {
    // 数据 
    taskList,
    page,
    pageSize,
    total,
    timeOrder,
    status,
    wholePaperPage,
    wholePaperPageSize,
    wholePaperTotal,
    wholePaperStatus,
    taskDetailList,
    doneTotal,
    singleQuestionPage,
    singleQuestionPageSize,
    singleQuestionTotal,
    uniquePaperIds,
    taskDetailListByTwo,
    reviewAssignmentId,
    isExpired,
    reviewAssignmentDetailIds,


    // 方法
    fetchTaskList,
    handleReset,
    handleQuery,
    fetchTaskDetailByOne,
    fetchTaskDetailByTwo,
    goToBatchReview,
    goToSinglePaperReview,
    goToSingleQuestionReview,
    goToSingleQuestionSinglePaperReview
  }
} 